package xyf.p11.other.twopointer;

import java.util.Arrays;

/**
 * ClassName: M1606_MinGap
 * Description:好难理解，太菜了
 * Author: xyf
 * Date: 9/9/22 10:23 AM
 * Version: 1.0
 **/
public class M1606_MinGap
{
    public int smallestDifference(int[] a, int[] b)
    {
        Arrays.sort(a);
        Arrays.sort(b);

        int i = 0;
        int j = 0;
        long min = Integer.MAX_VALUE;
        while (i < a.length && j < b.length)
        {
            long res = (long) a[i] - (long) b[j];
            if (res < 0)
            {
                min = Math.min(min, Math.abs(res));
                i++;
            }
            else if (res > 0)
            {
                min = Math.min(min, Math.abs(res));
                j++;
            }
            else
            {
                return 0;
            }

        }
        return (int) min;
    }
}
